package pe.com.csi.pm.regexiv;

import java.util.ArrayList;
import java.util.List;

import pe.com.csi.pm.regexiv.bean.Parametro;
import pe.com.csi.pm.regexiv.util.UUtil;
import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.Spinner;

public class TabCalzada extends Activity {

	private Spinner cmbTipo;
	private Spinner cmbTipo2;
	private Spinner cmbTipo3;
	private Spinner cmbTipo4;
	private EditText txtEspesor;
	private EditText txtModo;
	private EditText txtEspesor2;
	private EditText txtModo2;
	private EditText txtEspesor3;
	private EditText txtModo3;
	private EditText txtEspesor4;
	private EditText txtModo4;
	private EditText txtAncho;
	private EditText txtEstructura;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
		setContentView(R.layout.tab_calzada);

		cmbTipo = (Spinner) findViewById(R.id.cmbTipo);
		cmbTipo2 = (Spinner) findViewById(R.id.cmbTipo2);
		cmbTipo3 = (Spinner) findViewById(R.id.cmbTipo3);
		cmbTipo4 = (Spinner) findViewById(R.id.cmbTipo4);
		txtEspesor = (EditText) findViewById(R.id.cmbEspesor);
		txtModo = (EditText) findViewById(R.id.cmbModo);
		txtEspesor2 = (EditText) findViewById(R.id.cmbEspesor2);
		txtModo2 = (EditText) findViewById(R.id.cmbModo2);
		txtEspesor3 = (EditText) findViewById(R.id.cmbEspesor3);
		txtModo3 = (EditText) findViewById(R.id.cmbModo3);
		txtEspesor4 = (EditText) findViewById(R.id.cmbEspesor4);
		txtModo4 = (EditText) findViewById(R.id.cmbModo4);
		txtAncho = (EditText) findViewById(R.id.txtAncho);
		txtEstructura = (EditText) findViewById(R.id.txtEstructura);

		cargarCombo(cmbTipo, R.raw.tipo_uno_berma);
		cargarCombo(cmbTipo2, R.raw.tipo_uno_berma);
		cargarCombo(cmbTipo3, R.raw.tipo_dos_berma);
		cargarCombo(cmbTipo4, R.raw.tipo_tres_berma);

	}

	private void cargarCombo(Spinner cmb, int xml) {
		List<Parametro> lista = new ArrayList<Parametro>();
		lista.addAll(UUtil.obtenerParametrosEV(this, xml));

		ArrayAdapter<Parametro> adaptador = new ArrayAdapter<Parametro>(this, android.R.layout.simple_spinner_item, lista);
		adaptador.setDropDownViewResource(android.R.layout.simple_spinner_item);
		cmb.setAdapter(adaptador);
		cmb.setSelection(0);
	}

	@Override
	protected void onPause() {

		super.onPause();

		Parametro itemtipo1 = (Parametro) cmbTipo.getSelectedItem();
		Parametro itemtipo2 = (Parametro) cmbTipo2.getSelectedItem();
		Parametro itemtipo3 = (Parametro) cmbTipo3.getSelectedItem();
		Parametro itemtipo4 = (Parametro) cmbTipo4.getSelectedItem();

		SharedPreferences sp = getSharedPreferences("ELEMENTO_VIAL", MODE_PRIVATE);

		sp.edit().putString("ANCHOD", txtAncho.getText().toString()).commit();
		sp.edit().putString("ESTRUCTURAD", txtEstructura.getText().toString()).commit();
		sp.edit().putString("TIPO1D", itemtipo1.getKey().equalsIgnoreCase("0") ? null : itemtipo1.getValue()).commit();
		sp.edit().putString("TIPO2D", itemtipo2.getKey().equalsIgnoreCase("0") ? null : itemtipo2.getValue()).commit();
		sp.edit().putString("TIPO3D", itemtipo3.getKey().equalsIgnoreCase("0") ? null : itemtipo3.getValue()).commit();
		sp.edit().putString("TIPO4", itemtipo4.getKey().equalsIgnoreCase("0") ? null : itemtipo4.getValue()).commit();
		sp.edit().putString("ESPESOR1D", txtEspesor.getText().toString()).commit();
		sp.edit().putString("ESPESOR2D", txtEspesor2.getText().toString()).commit();
		sp.edit().putString("ESPESOR3D", txtEspesor3.getText().toString()).commit();
		sp.edit().putString("ESPESOR4", txtEspesor4.getText().toString()).commit();
		sp.edit().putString("MODO1D", txtModo.getText().toString()).commit();
		sp.edit().putString("MODO2D", txtModo2.getText().toString()).commit();
		sp.edit().putString("MODO3D", txtModo3.getText().toString()).commit();
		sp.edit().putString("MODO4", txtModo4.getText().toString()).commit();

	}

}